package com.example.sqlitedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sqlitedemo.db.DBService;
import com.example.sqlitedemo.db.Product;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button addBtn,updateBtn,deleBtn,queryBtn;
    private EditText edt_pname,edt_price;
    private TextView tv_show,tv_show2;
    DBService dbService;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=MainActivity.this;
        dbService=new DBService(context);

        initView();
    }
    private void initView(){
        addBtn=findViewById(R.id.addBtn);
        updateBtn=findViewById(R.id.updateBtn);
        deleBtn=findViewById(R.id.delBtn);
        queryBtn=findViewById(R.id.queryBtn);
        edt_pname=findViewById(R.id.edt_name);
        edt_price=findViewById(R.id.edt_money);
        tv_show=findViewById(R.id.tv_show);
        tv_show2=findViewById(R.id.tv_show2);
        tv_show.setText("编号       商品名称     商品价格");
        updateBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        deleBtn.setOnClickListener(this);
        queryBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.addBtn://添加
                String name =edt_pname.getText().toString().trim();
                String money = edt_price.getText().toString().trim();
                if (name.equals("") && money.equals("")){
                    Toast.makeText(context, "输入框不能为空", Toast.LENGTH_SHORT).show();
                } else {

                    Product p=new Product();
                    p.setProductName(name);
                    p.setPrice(Double.parseDouble(money));
                    dbService.insert(p);

                    Toast.makeText(context, "添加商品成功", Toast.LENGTH_SHORT).show();
                    edt_pname.setText("");
                    edt_price.setText("");
                }


                break;
            case R.id.updateBtn://更新
                    Product product1=new Product();
                    product1.setPid(1);
                    product1.setPrice(66f);
                    dbService.updatePrice(product1);
                    Toast.makeText(context, "修改商品价格成功", Toast.LENGTH_SHORT).show();


                break;
            case R.id.delBtn://删除
                    int result=dbService.deletProduct(2);
                    if(result>0){
                            Toast.makeText(context, "删除商品成功", Toast.LENGTH_SHORT).show();
                    }else {
                            Toast.makeText(context, "删除商品不成功", Toast.LENGTH_SHORT).show();
                    }

                break;
            case R.id.queryBtn://查询
                List<Product> lists=dbService.findAll();
                if(lists.size()>0){
                    tv_show2.setText("");
                    for(Product product:lists) {
                        tv_show2.append(" " + product.getPid() + "\t\t\t\t\t" + product.getProductName() +
                                "\t\t\t\t\t" + product.getPrice() + "\n");
                    }
                }else{
                    tv_show.setText("暂无数据");
                }
                break;

        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        dbService.closeDB();//关闭数据库
    }
}